<?php
class M_DbTable_Country extends Apps_Db_Table_Abstract {
	protected $_name = 'country';

	const STATUS_ACTIVE 	= 1;
	const STATUS_INACTIVE 	= 0;
	const STATUS_DELETED 	= -1;

	const DEFAULT_VIETNAM_COUNTRY_ID = 1;

	//LIST
	public function getList () {
		$q = $this->_db
				  ->select()
				  ->from($this->_name, null)
				  ->where('status =?', self::STATUS_ACTIVE)
				  ->columns(array('id'			    => 'id',
				 				  'name'		    => 'name',
				 				  'en_name'		    => 'en_name',
				 				  'description'	    => 'description',
				 				  'sorting_number'	=> 'sorting_number',
				  				  'created_date'    => 'created_date'));
 		return $q;
	}

	//get 1 country, article by countryId
	public function getCountry($countryId) {
	    $q = $this->_db
	    ->select()
	    ->from(array('c'   => $this->_name), null)
	    ->joinLeft(array('a' 	 => 'article'), 'a.reference_id = c.id', null)
	    ->where('a.reference_type =?', M_DbTable_Article::REFERENCE_TYPE_COUNTRY)
	    ->where('c.id =?', $countryId)
	    ->columns(
	            array('id'				=> 'c.id',
	                  'name'			=> 'c.name',
	                  'en_name'			=> 'c.en_name',
	                  'sorting_number'	=> 'c.sorting_number',
	                  'description'		=> 'c.description',
	                  'title'			=> 'a.title',
	                  'en_title'		=> 'a.en_title',
	                  'detail'			=> 'a.detail',
	                  'en_detail'		=> 'a.en_detail',
	            ));
	    return $q;
	}

	//get list country other
	public function getListOther($countryId) {
		$q = $this->_db
				  ->select()
				  ->from($this->_name, null)
				  ->where('country.status = ?', self::STATUS_ACTIVE)
				  ->where('country.id != ?', $this->_db->quote($countryId))
				  ->columns(array('id'			    => 'id',
				 				  'en_name'		    => 'en_name'));
 		return $q;
	}

}